package com.sgb.jicai.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.jicai.BR;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ItemFactoryCentralizedList2Binding;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.swgk.core.base.aroute.ARouterStartWebBase;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

//供方发起列表适配器
public class FactoryCentralizedListItemAdapter2 extends BaseRecycleAdapter<FactoryCentralizedItemEntity> {
    private Context context;
    private boolean isVisible; // 是否显示进入商店入口

    public FactoryCentralizedListItemAdapter2(Context context, List<FactoryCentralizedItemEntity> datas, String type) {
        super(context, R.layout.item_factory_centralized_list2, datas);
        this.context = context;
        if (type.equals("1")) {
            isVisible = true;
        } else {
            isVisible = false;
        }
    }

    @Override
    public void convert(ItemViewHolder holder, final FactoryCentralizedItemEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        //进店按钮
        ((ItemFactoryCentralizedList2Binding) holder.getmBinding()).ent.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        ((ItemFactoryCentralizedList2Binding) holder.getmBinding()).ent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(entity.getCompanyNo())) {
                    ARouterStartWebBase.startWebSiteInfo(entity.getCompanyNo(),1);
                } else {
                    MToast.showToast(context, "获取企业信息失败！");
                }
            }
        });

        updateRecycler(((ItemFactoryCentralizedList2Binding) holder.getmBinding()).childRecycler, entity);
        updateImageStatus(((ItemFactoryCentralizedList2Binding) holder.getmBinding()).imgTab, entity);
    }

    private void updateRecycler(RecyclerView recyclerView, FactoryCentralizedItemEntity entity) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        FactoryCentralizedListItemChildAdapter childAdapter = new FactoryCentralizedListItemChildAdapter(recyclerView.getContext(), entity.getProductSaleSlipDetails());
        recyclerView.setAdapter(childAdapter);
        childAdapter.setOnItemClick(new AdapterOnItemClick<FactoryCentralizedItemEntity.ProductSaleSlipDetails>() {
            @Override
            public void onItemClick(FactoryCentralizedItemEntity.ProductSaleSlipDetails productSaleSlipDetails, int position) {
                if (onItemClick != null) {
                    onItemClick.onItemClick(entity, position);
                }
            }
        });
    }

    private void updateImageStatus(ImageView imageView, FactoryCentralizedItemEntity entity) {
        // 集采状态状态1 集采中 2 集采完成
        if (entity.getStatus() == 1) {
            switch (entity.getPriceWarning()) {//价格预警 1 大于 2 等于 3 小于
                case 1:
                    imageView.setImageResource(R.drawable.jcz_dy);
                    break;
                case 2:
                    imageView.setImageResource(R.drawable.jcz_dengyu);
                    break;
                case 3:
                    imageView.setImageResource(R.drawable.jcz_xy);
                    break;
                default:
                    imageView.setImageResource(R.drawable.jcz_dengyu);
                    break;
            }
        } else {
            imageView.setImageResource(R.drawable.icon_jcwc);
        }
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
    }

}
